import requests
import os

image_path = input('>>> Original image path: ').removeprefix(
    '"').removesuffix('"')
image_dir = os.path.dirname(image_path)
image_title = image_path.split('.')[-2]

print('Removing background...')

with open(image_path, 'rb') as fr:
    response = requests.post(
        'https://api.remove.bg/v1.0/removebg',
        files={'image_file': fr},
        data={'size': 'auto'},
        headers={'X-Api-Key': '9B9qRbfEFYksrmJNZrkZKvJu'},
    )

if response.status_code == 200:
    print('Saving...')
    with open(os.path.join(image_dir, f'{image_title}-removebg.png'), 'wb') as fr:
        fr.write(response.content)
else:
    print(f'Error {response.status_code}: {response.text}')
